<!--/**
* @author xuyh0817
* @email xuyh0817@foxmail.com
* @date 2022-02-16 15:58
* @version 2.0.0
* @doc todo
* @website todo
* @remark  如果要分发源码，需在本文件顶部保留此文件头信息！！
*/
-->
<template>
  <a-radio-group
      :value="value"
      v-bind="attrs"
      :name="radioName"
      @change="radioChangeHandler"
  >
    <template v-for="(item,index) in children">
      <a-radio-button
          v-if="isButton"
          :key="index"
          v-bind="item"
      >
        {{ item.label }}
      </a-radio-button>
      <a-radio
          v-else
          :key="index"
          v-bind="item"
      >
        {{ item.label }}
      </a-radio>
    </template>
  </a-radio-group>
</template>

<script>
import { COMPONENT_TYPE, CUSTOM_EVENT_TYPE } from '../../constants'
import ComponentMixin from '../../XRenderMixins/XRenderComponentMixin'

export default {
  name: 'AntFormBaseRadio',
  mixins: [ComponentMixin],
  props: {
    value: {
      type: [String, Number, Boolean]
    },
    isButton: {
      type: Boolean,
      default: false
    },
    componentType: {
      type: String,
      default: COMPONENT_TYPE.RADIO
    }
  },
  computed: {
    children: {
      get() {
        const {attrs, isDebug, formItem} = this
        const children = attrs.children
        if (typeof (children) !== 'string' && !(children instanceof Array)) {
          isDebug ? console.warn(`${formItem.prop} children类型错误,非String或Array`) : ''
          return []
        }
        if (children instanceof Array) {
          return children || []
        }
        return (new Function('return ' + children))()
      }
    },
    radioName: {
      get() {
        const {attrs, formItem} = this
        if (!!attrs && !!attrs.name) {
          return attrs.name + Date.now()
        }
        if (!!formItem && !!formItem.prop) {
          return formItem.prop + Date.now()
        }
        return Date.now() + ''
      }
    }
  },
  methods: {
    radioChangeHandler(event) {
      const {attrs, formItem, isDebug, componentType, children} = this
      const value = event.target.value
      // 为关联的键进行赋值
      // select的option
      const obj = {}

      // 关联的key
      let {associations} = attrs

      if (typeof (associations) !== 'string' && !(associations instanceof Array)) {
        isDebug ? console.warn(`${formItem.prop} associations类型错误,非String或Array`) : ''
        associations = []
      }
      if (typeof (associations) === 'string') {
        associations = (new Function('return ' + associations))()
      }
      if (associations instanceof Array) {
        associations = associations || []
      }

      // 关联的key
      for (const child of children) {
        if (child.value === value) {
          for (const association of associations) {
            if (!!association.key && !!association.mapKey) {
              obj[association.key] = child[association.mapKey]
            }
          }
        }
      }

      this.$emit(CUSTOM_EVENT_TYPE.CUSTOM_EVENT, {
        type: CUSTOM_EVENT_TYPE.ASSOCIATION_CHANGE_EVENT,
        compType: componentType,
        prop: formItem ? this.formItem.prop : '',
        value: obj,
        attrs: attrs,
        formItem: formItem,
        event
      })


      this.$emit(CUSTOM_EVENT_TYPE.CHANGE, {
        type: componentType,
        prop: formItem ? formItem.prop : '',
        value,
        attrs,
        formItem,
        association: obj,
        event
      })
    }
  }
}
</script>

<style scoped>

</style>
